<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Buy_books extends MIS_Controller 
{
	private $per_page = 12;
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/buy_books
	 *	- or -  
	 * 		http://example.com/buy-books
	 *	- or -  
	 * 		http://example.com/buy_books/index
	 *	- or -  
	 * 		http://example.com/buy-books/index
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$this->load->library('pagination');
		
		$search_others=$this->input->post('search_others');
		$search_campus=$this->input->post('search_campus');
		$page=$this->uri->segment(4);
		if(is_numeric($page) && $page > 0) $page = ceil($page);
		else $page = 1;
		if(!empty($search_campus)|| !empty($search_others))
		{
			$this->db->select('*');
			$this->db->from('book_bank');
			$this->db->like('Campus', $this->input->post('search_campus'));
			$this->db->or_like('ISBN', $this->input->post('search_others'));
			$this->db->or_like('Name', $this->input->post('search_others'));
			$this->db->or_like('University', $this->input->post('search_others'));
			
			$query = $this->db->get();
			$config['total_rows'] = $query->num_rows();
			$query->free_result();
			$query = $this->db->query($this->db->last_query() . sprintf(" LIMIT %d, %d", ($page-1)*$this->per_page, $this->per_page));
			unset($_POST);
		}
		else 
		{
			$query = $this->db->get('book_bank');
			$config['total_rows'] = $query->num_rows();
			$query->free_result();
			$query = $this->db->query($this->db->last_query() . sprintf(" LIMIT %d, %d", (($page-1)*$this->per_page), $this->per_page));
		}
		
		$config['base_url'] = base_url('buy-books/index/page/');
		$config['per_page'] = $this->per_page;
		$config['use_page_numbers'] = TRUE;
		$config['uri_segment'] = 4;
		$config['first_link'] = 'First';
		$config['first_tag_open'] = '<span class="prev">';
		$config['first_tag_close'] = '</span>';
		$config['last_link'] = 'Last';
		$config['last_tag_open'] = '<span class="page_link last_link">';
		$config['last_tag_close'] = '</span>';
		$config['cur_tag_open'] = '<span class="block">';
		$config['cur_tag_close'] = '</span>';
		$config['num_tag_open'] = '<span class="page_link">';
		$config['num_tag_close'] = '</span>';
		$config['next_link'] = 'Next';
		$config['next_tag_open'] = '<span class="page_link gap_link">';
		$config['next_tag_close'] = '</span>';
		$config['prev_link'] = 'Prev';
		$config['prev_tag_open'] = '<span class="page_link gap_link">';
		$config['prev_tag_close'] = '</span>';
		$this->pagination->initialize($config);
		$this->loadViewData['pagination'] = $this->pagination->create_links();
		
		$res=$query->result();
		$this->load->helper('text');
		$this->loadViewData['banner_inc'] = false;
		$this->loadViewData['upper_footer_inc'] = false;
		$this->loadViewData['viewFile'] = 'v_buy_books';
		$this->loadViewData['book_details'] = $res;
		$this->addCss('css/pagenization.css'); 
		$this->load->view('layout', $this->loadViewData);
	}
	public function books()
	{
		$book_url=$this->uri->segment(4);
		$book_id=$this->uri->segment(3);
		if(!ctype_digit($book_id) || empty($book_url))
		{
			show_error("Opps! you are in wrong place.", 401, "Invalid access");
		}
		
		$query = $this->db->get_where('book_bank', array('id'=>"$book_id"));
		$res=$query->result();
		$this->db->order_by("id", "random");
		$this->db->limit(3);
		$query_cross_sell = $this->db->get('book_bank');
		$res_cross_sell=$query_cross_sell->result();
		$this->load->helper('text');
		$this->loadViewData['banner_inc'] = false;
		$this->loadViewData['upper_footer_inc'] = false;
		$this->loadViewData['viewFile'] = 'v_buy_books_details';
		$this->loadViewData['book_details'] = $res;
		$this->loadViewData['cross_sell'] = $res_cross_sell;
		$this->addCss('css/pagenization.css');
		$this->load->view('layout', $this->loadViewData);
	}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */